/** 创建组件对象 * */
var reportForm = new Form();
var reportTable = new Table();

/**
 	页面加载完执行
	@param
	@return
 */
$(function(){
	initConditions();
	initButtons();
	initTitle();
	initReport();
});

/**
	初始化查询条件
	@param
	@return
*/
function initConditions() {
	reportForm.initTableConditions({
		divId: "reportQueryDiv",
		conditions: [{
			width: "220",
			label: {
				width: "65",
				text: "月份"
			},
			input: {
				width: "155",
				type: "text",
				key: "month"
			}
		}],
		button: {
			action: "yddemo:report:view",
			width: "120",
			align: "center",
			label: "查询",
			clickFun : function(){
				//查询方法配置
			}
		}
	});
}

/**
	初始化按钮
	@param
	@return
*/
function initButtons(){
	reportTable.initButtons({
		divId: "reportButtonDiv",
		buttons: [{
			text: "保存",
			icon: staticPath+"/skin/"+skinType+"/icon/Button_main_save.png",
			action: "yddemo:report:save",
			clickFun: "addNewuser()"
		},{
			text: "导出",
			icon: staticPath+"/skin/"+skinType+"/icon/Button_main_export.png",
			action: "system:newuser:edit",
			clickFun: "updateNewuser()"
		},{
			text: "更新",
			icon: staticPath+"/skin/"+skinType+"/icon/Button_main_refresh.png",
			action: "system:newuser:delete",
			clickFun: "deleteNewuser()"
		}]
	});
}

/**
	初始化报表
	@param
	@return
*/
function initTitle(){
	var temp_head = [{
		width: 260,
		height: 25,
		text: "",
		key: [{
			width: 260,
			height: 25,
			text: "广东粤电集团有限公司",
			key: [{
				width: 260,
				height: 25,
				text: "9808000001",
				key: [{
					width: 260,
					height: 25,
					text: "统计专贡",
					key: [{
						width: 260,
						height: 25,
						text: "C107-6000千瓦及以上电厂发电生产情况(年报)",
						key: [{
							width: 260,
							height: 25,
							text: "425000",
							key: [{
								width: 260,
								height: 25,
								text: "1",
								key: [{
									width: 260,
									height: 25,
									text: "2015",
									key: ""
								}]
							}]
						}]
					}],
					style: "headColor:#dd0000;"
				}],
				style: "headColor:#dd0000;"
			}],
			style: "headColor:#dd0000;"
		}]
	},{
		width: 70,
		height: 25,
		text: "",
		key: [{
			width: 70,
			height: 25,
			text: "（单位名称）",
			key: [{
				width: 70,
				height: 25,
				text: "（单位代码）",
				key: [{
					width: 70,
					height: 25,
					text: "（填报人）",
					key: [{
						width: 70,
						height: 25,
						text: "（表名）",
						key: [{
							width: 70,
							height: 25,
							text: "（表号）",
							key: [{
								width: 70,
								height: 25,
								text: "（期别）",
								key: [{
									width: 70,
									height: 25,
									text: "（年）",
									key: ""
								}]
							}]
						}]
					}]
				}]
			}]
		}]
	},{
		width: 240,
		height: 175,
		text: "1.本表头供系统识别用<br/>2.各单位根据具体情况填写单位代码、单位名称、填报人<br/>3.本表头报告期必须与此报表报告期一致<br/>4.指标代码必须按规律编<br/>5.不可改动报表格式",
		key: [{
			width: 80,
			height: 25,
			text: "12",
			key: ""
		},{
			width: 80,
			height: 25,
			text: "月",
			key: ""
		},{
			width: 80,
			height: 25,
			text: "aa",
			key: ""
		}]
	}];
	var temp_data = [];
	
	loadTable("titleTableDiv", "", temp_head, temp_data);
}

/**
	初始化报表
	@param
	@return
*/
function initReport(){
	var temp_title = "6000千瓦及以上电厂发电生产情况";
	var temp_head = [{
		width: 100,
		height: 90,
		text: "指标名称",
		key: "zbmc",
		style: "align:left;"
	},{
		width: 100,
		height: 90,
		text: "指标代码",
		key: "zbdm",
		style: "align:left;"
	},{
		width: 100,
		height: 90,
		text: "组织机构代码",
		key: "zzjgdm",
		style: "align:left;"
	},{
		width: 100,
		height: 60,
		text: "发电厂个数",
		key: [{
			width: 100,
			height: 30,
			text: "(个)",
			key: "fdcgs",
			style: "align:right;"
		}]
	},{
		width: 230,
		height: 30,
		text: "期末发电设备容量",
		key: [{
			width: 80,
			height: 60,
			text: "(千瓦)",
			key: "qmfdsbrl1",
			style: "align:right;"
		},{
			width: 150,
			height: 60,
			text: "其中:供热设备容量",
			key: "qmfdsbrl2",
			style: "align:right;"
		}]
	},{
		width: 320,
		height: 30,
		text: "发电量(万千瓦时)",
		key: [{
			width: 160,
			height: 30,
			text: "本月",
			key: [{
				width: 80,
				height: 30,
				text: "合计",
				key: "fdl11",
				style: "align:right;"
			},{
				width: 80,
				height: 30,
				text: "试运行",
				key: "fdl12",
				style: "align:right;"
			}]
		},{
			width: 160,
			height: 30,
			text: "累计",
			key: [{
				width: 80,
				height: 30,
				text: "合计",
				key: "fdl21",
				style: "align:right;"
			},{
				width: 80,
				height: 30,
				text: "试运行",
				key: "fdl22",
				style: "align:right;"
			}]
		}]
	},{
		width: 160,
		height: 30,
		text: "上网电量",
		key: [{
			width: 160,
			height: 30,
			text: "(万千瓦时)",
			key: [{
				width: 80,
				height: 30,
				text: "本月",
				key: "swdl1",
				style: "align:right;"
			},{
				width: 80,
				height: 30,
				text: "累计",
				key: "swdl2",
				style: "align:right;"
			}]
		}]
	},{
		width: 160,
		height: 30,
		text: "平均利用小时",
		key: [{
			width: 160,
			height: 30,
			text: "(小时)",
			key: [{
				width: 80,
				height: 30,
				text: "本月",
				key: "pjlixs1",
				style: "align:right;"
			},{
				width: 80,
				height: 30,
				text: "累计",
				key: "pjlixs2",
				style: "align:right;"
			}]
		}]
	},{
		width: 160,
		height: 30,
		text: "平均设备容量",
		key: [{
			width: 160,
			height: 30,
			text: "(千瓦)",
			key: [{
				width: 80,
				height: 30,
				text: "本月",
				key: "pjsbrl1",
				style: "align:right;"
			},{
				width: 80,
				height: 30,
				text: "累计",
				key: "pjsbrl2",
				style: "align:right;"
			}]
		}]
	}];
	var temp_data = [{ //Ajax
		id: "100001",
		zbmc: "一、合计",
		zbdm: "01",
		zzjgdm: null,
		fdcgs: 42,
		qmfdsbrl1: 29649100,
		qmfdsbrl2: null,
		fdl11: null,
		fdl12: null,
		fdl21: 10550788,
		fdl22: 29055,
		swdl1: null,
		swdl2: 9946196,
		pjlixs1: null,
		pjlixs2: 3607,
		pjsbrl1: null,
		pjsbrl2: 29251400
	},{
		id: "100002",
		zbmc: "（一）水电",
		zbdm: "0101",
		zzjgdm: null,
		fdcgs: 12,
		qmfdsbrl1: 2184800,
		qmfdsbrl2: null,
		fdl11: null,
		fdl12: null,
		fdl21: 941053,
		fdl22: null,
		swdl1: null,
		swdl2: 932498,
		pjlixs1: null,
		pjlixs2: 4307,
		pjsbrl1: null,
		pjsbrl2: 2184800
	}];
	
	loadTable("reportTableDiv", temp_title, temp_head, temp_data);
}

/**
	加载表格
	@param
	@return
*/
function loadTable(divId, title, headArr, dataArr){
	//1.标题
	$("#"+divId).append("<div style='text-align:center;'>"+title+"</div>");
	//2.表头
	var $table = $("<div class='reportTable'></div>").appendTo($("#"+divId));
	var $thead = $("<div class='theadDiv'></div>");
	for (var i=0; i<headArr.length; i++){
		var $th = $("<th class='bb-gray'></th>").append(getHeadTh(headArr[i], true));
		$thead.append($th);
	}
	$table.append($thead);
	var $lastThDiv = $thead.find("th:last").children("div");
	//$lastThDiv.addClass("br-gray");
	//$lastThDiv.width($lastThDiv.width()-1);
	//$lastThDiv.children("div:last").width($lastThDiv.children("div:last").width()-1);
	
	//3.表数据
	var $tbody = $("<div class='tbodyDiv'></div>");
	for (var row=0; row<dataArr.length; row++){
		var obj = dataArr[row];
		var $bodyTr = $("<tr></tr>");
		var columns = $thead.find(".headTextDiv");
		columns.each(function(col, value){
			var key = $(this).attr("key");
			if (key == undefined){
				return true;
			}
			var $text = $("<input type='text' class='magusText' style='padding:0 5px;'></input>").width($(this).width()).val(obj[key]);
			var align = getParm($(this).attr("myStyle"), "align");
			if (align != ""){
				$text.css("text-align", align);
			}
			var $textDiv = $("<div></div>").width($(this).width()).append($text);
			var $td = $("<td class='bt-gray bl-gray'></td>").append($textDiv);
			if (col == columns.length-1){
				$td.addClass("br-gray");
				$td.children("div").width($td.children("div").width()-2);
			}
			if (row == dataArr.length-1){
				$td.addClass("bb-gray");
			}
			$bodyTr.append($td);
		});
		$tbody.append($bodyTr);
	}
	$table.append($tbody);
}

function getParm(content, key){
	if (content == undefined || content == ""){
		return "";
	}
	var content = content.substr(content.indexOf(key)+key.length+1);
	return content.substr(0, content.indexOf(";"));
}

/**
	加载表格
	@param
	@return
*/
function getHeadTh(obj, isRoot){
	var $div = $("<div style='float:left;'></div>").width(obj.width);
	var headColor = getParm(obj.style, "headColor");
	if (typeof(obj.key) == "string"){
		$div.append(obj.text).attr({"key":obj.key, "myStyle":obj.style}).addClass("headTextDiv bt-gray bl-gray").css({"height":obj.height+"px", "line-height":obj.height+"px"});
		if (headColor != ""){
			$div.css("color", headColor);
		}
	} else if (typeof(obj.key) == "object"){
		var $childDiv = $("<div class='headTextDiv bt-gray bl-gray' style='float:left;' myStyle='"+obj.style+"'>"+obj.text+"</div>").width(obj.width).height(obj.height);
		if (headColor != ""){
			$childDiv.css("color", headColor);
		}
		if (obj.text.indexOf("<br/>") == -1){
			$childDiv.css("line-height", obj.height+"px");
		} else{
			$childDiv.css({"text-align":"left","padding":"10px 0 0 10px"});
		}
		$div.append($childDiv);
		for (var i=0; i<obj.key.length; i++){
			$div.append(getHeadTh(obj.key[i], false));
		}
	}
	return $div;
}



